/*******************************************************
> Copyright (C) 2024 ==IOT== All rights reserved.
> File Name: likang2.c
> Author: lv1
> Website:www.lv1.org
> Created Time: 2024年12月06日 星期五 09时17分56秒
***************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "cJSON.h"
#include <mysql/mysql.h>

char *packJson();
void parseJson(char *p);
int main(int argc, const char* argv[])
{
	char*p =packJson();
	parseJson(p);
	free(p);
    return 0;
}

char *packJson() {
    // 创建 JSON 对象
    cJSON* root = cJSON_CreateObject();
	
	cJSON* type = cJSON_CreateString("delete_files");
	char name[20] = {0};
	printf("input delete name: ");
	scanf("%s",name);
	cJSON* cjson_name = cJSON_CreateString(name);
	
	cJSON_AddItemToObject(root, "type",type);
	cJSON_AddItemToObject(root, "name",cjson_name);
	
	char *p = (char*)malloc(1024);
	p = cJSON_Print(root);
	
	printf("%s\n",p);
	cJSON_Delete(root);
	
	return p;
}

void parseJson(char *p){
	
    cJSON *root = cJSON_Parse(p);
    

    cJSON *type = cJSON_GetObjectItem(root, "type");
    cJSON *name = cJSON_GetObjectItem(root, "name");
    
    char *type1 = cJSON_GetStringValue(type);
    char *name1 = cJSON_GetStringValue(name);
   
    printf("type:%s-name:%s\n", type1, name1);
    cJSON_Delete(root);
	/*
	MYSQL mysql;
	mysql_init(&mysql);
	MYSQL *flag;
	flag = mysql_real_connect(&mysql,"localhost","root","1","care",0,NULL,0);
	char delsql[1000]={0};
	sprintf(delsql,"delete from old_files where name = '%s'",name1);
	mysql_query(&mysql,delsql);
	
	mysql_close(&mysql);
	*/
}
